package com.un.ebs.customer.controller;


import com.un.ebs.core.enums.BooleanEnum;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.customer.CustomerConfigureCode;
import com.un.ebs.customer.domain.CustomerTraceBookExt;
import com.un.ebs.customer.domain.CustomerTraceExt;
import com.un.ebs.customer.dto.CustomerBookDto;
import com.un.ebs.customer.dto.CustomerBookRequest;
import com.un.ebs.customer.dto.CustomerTraceAddDto;
import com.un.ebs.customer.service.CustomerTraceService;
import com.un.ebs.product.dto.ProductCanBookSpuTimeResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-09-24
 */
@RestController
@RequestMapping("/customerTrace")
@Api(value = "customer-trace", description = "客户跟进")
public class CustomerTraceController {

    @Autowired
    private CustomerTraceService service;

    @PostMapping("add")
    @ApiOperation(value = "add", notes = "添加")
    public WebResult save(@RequestBody @Validated CustomerTraceAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("addBook")
    @ApiOperation(value = "创建预约")
    public WebResult addBook(@RequestParam String custId, @RequestParam Date time, @RequestParam String desc) {
        service.addBook(custId, CustomerConfigureCode.customer_trace_dict_book_default, time, desc);
        return WebUtils.success();
    }

    @PostMapping("reply")
    @ApiOperation(value = "客服回复")
    public WebResult reply(@RequestParam String traceId, @RequestParam String remark) {
        service.reply(traceId, remark);
        return WebUtils.success();
    }

    @PostMapping("visit")
    @ApiOperation(value = "回访记录")
    public WebResult visit(@RequestParam String traceId, @RequestParam String remark) {
        service.visit(traceId, remark);
        return WebUtils.success();
    }

    @PostMapping("addBookBySpu")
    @ApiOperation(value = "创建预约，根据Spu")
    public WebResult addBook(@RequestBody @Validated CustomerBookRequest req) {
        service.addBook(req);
        return WebUtils.success();
    }

    @PostMapping("addBookOrder")
    @ApiOperation(value = "创建预约订单")
    public WebResult createOrderByBook(@RequestParam String bookId, @RequestParam String skuId, @RequestParam(required = false) String tombSkuId, @RequestParam int qty) {
        return WebUtils.success(service.createOrderByBook(bookId, skuId, tombSkuId, qty));
    }

    @PostMapping("completeBook")
    @ApiOperation(value = "completeBook", notes = "完成预约")
    public WebResult completeBook(@RequestParam String traceId) {
        service.completeBook(traceId);
        return WebUtils.success();
    }

    @PostMapping("updateBook")
    @ApiOperation(value = "updateBook", notes = "预约调整")
    public WebResult updateBook(@RequestParam String traceId, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time, @RequestParam(required = false) String remark) {
        service.updateBookTime(traceId, time, remark);
        return WebUtils.success();
    }

    @PostMapping("cancelBook")
    @ApiOperation(value = "cancelBook", notes = "取消预约")
    public WebResult cancelBook(@RequestParam String traceId, @RequestParam(required = false) String remark) {
        service.cancelBook(traceId, remark);
        return WebUtils.success();
    }

    @GetMapping("listBook")
    @ApiOperation(value = "查询客户预约记录")
    public WebResult<List<CustomerBookDto>> listBook(@RequestParam(required = false) @ApiParam("如果为空，则默认取当前用户的预约记录") String custId) {
        return WebUtils.success(service.queryBook(custId));
    }

    @GetMapping("listAllBook")
    @ApiOperation(value = "查询客户预约记录")
    public WebResult<List<CustomerTraceBookExt>> listAllBook(@RequestParam(required = false) String searchText, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(service.queryAllBook(searchText, pageSize, pageIndex));
    }

    @GetMapping("listCanBookSpuTime")
    @ApiOperation(value = "查询项目的可预约时段")
    public WebResult<List<ProductCanBookSpuTimeResponse>> queryCanBookSpuTime(@RequestParam String spuId, @RequestParam Date day) {
        return WebUtils.success(service.queryCanBookSpu(spuId, day));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getById", notes = "根据主键获取单个 CustomerTrace")
    public WebResult getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "查询，客户接触记录", notes = "")
    public WebResult<List<CustomerTraceExt>> list(@RequestParam(required = false) String customerId, @RequestParam String tractDict, @RequestParam(required = false) BooleanEnum isPlan, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(service.queryByTraceType(customerId, tractDict, isPlan == null ? BooleanEnum.Default : isPlan, pageSize, pageIndex));
    }

}